package com.lindroy.androidplayer.ui.home.fragment

import android.os.Bundle
import android.text.Html
import android.view.LayoutInflater
import androidx.fragment.app.viewModels
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.BaseLazyFragment
import com.lindroy.androidplayer.base.appViewModel
import com.lindroy.androidplayer.base.eventViewModel
import com.lindroy.androidplayer.extensions.*
import com.lindroy.androidplayer.logic.model.BannerResp
import com.lindroy.androidplayer.logic.model.bean.CollectBus
import com.lindroy.androidplayer.logic.model.bean.ContentBean
import com.lindroy.androidplayer.ui.adapter.HomeDataAdapter
import com.lindroy.androidplayer.ui.webview.ContentActivity
import com.lindroy.androidplayer.viewmodel.CollectViewModel
import com.lindroy.androidplayer.viewmodel.HomeDataViewModel
import com.lindroy.lib_utils.extensions.setGone
import com.lindroy.lib_utils.extensions.setVisible
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerImageAdapter
import com.youth.banner.holder.BannerImageHolder
import com.youth.banner.indicator.CircleIndicator
import kotlinx.android.synthetic.main.fragment_home.*


/**
 * @author Lin
 * @date 2021/4/7
 * @function 首页
 * @Description
 */
class HomeFragment(override val layoutId: Int = R.layout.fragment_home) : BaseLazyFragment() {

    private val homeViewModel: HomeDataViewModel by viewModels()

    private val collectViewModel: CollectViewModel by viewModels()

    private lateinit var banner: Banner<BannerResp, BannerImageAdapter<BannerResp>>

    private val adapter: HomeDataAdapter by lazy { HomeDataAdapter() }

    override fun initView() {
        addLoadingObserve(collectViewModel)
        val bannerView = LayoutInflater.from(context).inflate(R.layout.banner, null)
        banner =
            bannerView.findViewById<Banner<BannerResp, BannerImageAdapter<BannerResp>>>(
                R.id.banner
            )
        refreshHeader.initColor()
        rvArticle.setLineDivider()
        rvArticle.adapter = adapter
        adapter.addHeaderView(bannerView)
    }

    override fun lazyLoad() {
        reqHomeData(true)
        homeViewModel.getBanner()
    }

    override fun initListener() {
        refreshLayout.setOnRefreshListener {
            reqHomeData(false)
        }
        refreshLayout.setOnLoadMoreListener {
            reqHomeData(false)
        }
        homeViewModel.homeState.observe(this) { data ->
            loadListData(data, adapter, refreshLayout, stateLayout)
        }
        //收藏的监听
        eventViewModel.collectEvent.observeInFragment(this) { collectBus ->
            val index = adapter.data.indexOfFirst {
                it.id == collectBus.originId
            }
            if (index >= 0) {
                adapter.data[index].collect = collectBus.collect
                adapter.notifyItemChanged(index + adapter.headerLayoutCount)
            }
        }
        appViewModel.loginInfo.observeInFragment(this){loginResp->
            globalLoginInfo.isLogin.not().yes {
                //退出登录的话把收藏都去掉
                adapter.data.forEach {
                    it.collect.yes {
                        it.collect = false
                    }
                }
                adapter.notifyDataSetChanged()
            }
        }
        homeViewModel.bannerData.observe(this) { resultState ->
            parseState(resultState) {
                it.isNotEmpty().yes {
                    banner.setVisible()
                    banner.setAdapter(object : BannerImageAdapter<BannerResp>(it) {
                        override fun onBindView(
                            holder: BannerImageHolder,
                            data: BannerResp,
                            position: Int,
                            size: Int
                        ) {
                            data.imagePath.isNotEmpty().yes {
                                holder.imageView.loadUrl(data.imagePath)
                                holder.imageView.setOnClickListener {
                                    context?.let { it1 ->
                                        ContentActivity.start(
                                            it1,
                                            data.id,
                                            data.url,
                                            data.title,
                                            false,
                                            ContentBean.TYPE_URL
                                        )
                                    }
                                }
                            }
                        }
                    }).addBannerLifecycleObserver(this).indicator = CircleIndicator(context)
                }.no {
                    banner.setGone()
                }
            }

        }

        collectViewModel.collectState.observe(this) {
            if (it.success) {
                eventViewModel.collectEvent.value = CollectBus(it.id, it.collect, it.originId)
            }
        }
    }

    override fun initClick() {
        adapter.setOnItemClickListener { _, view, position ->
            val item = adapter.data[position]
            ContentActivity.start(view.context, item.id, item.link, item.title, item.collect)
        }
        adapter.setOnCollectClickListener { collect, id ->
            collectViewModel.toggleCollectListArticle(collect, id)
        }
        stateLayout.setOnViewsClickListener { state, view ->
            homeViewModel.getBanner()
            reqHomeData(showLoading = true,isRefresh = true)
        }
    }

    private fun reqHomeData(showLoading:Boolean,isRefresh:Boolean = true){
        if (showLoading){
            stateLayout.showLoading()
        }
        homeViewModel.reqHomeArticles(isRefresh)

    }

    companion object {
        @JvmStatic
        fun newInstance() =
            HomeFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }

}
